package com.account.order.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.account.mapper.TAccountMapper;
import com.account.domain.TAccount;
import com.account.order.service.impl.TAccountService;
import spring.cloud.Utils.R;
import sun.nio.ch.DefaultSelectorProvider;

import java.util.List;
import java.util.concurrent.TimeUnit;

/**
 * @author cyt
 * @create 2020-06-07 10:16
 */
@Service
public class TAccountServiceImpl extends ServiceImpl<TAccountMapper, TAccount> implements TAccountService {
    @Override
    public R dec(Integer userId, Integer num) {
        QueryWrapper<TAccount> wrapper = new QueryWrapper<TAccount>();
        wrapper.eq("user_id", userId);
        TAccount account = baseMapper.selectOne(wrapper);
        int used = account.getUsed() + num;
        int ans = account.getResidue() - num;
        account.setResidue(ans);
        account.setUsed(used);
        /**
         * 睡眠
         */
        try {
            TimeUnit.SECONDS.sleep(20);
        } catch (Exception e) {
            log.debug(e.getMessage());
        }

        baseMapper.update(account, wrapper);
        return R.ok();
    }

    @Override
    public List<TAccount> getAll() {
        return baseMapper.selectAll();
    }
}
